package xupt.se.ttms.view.movie;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.*;

import xupt.se.ttms.model.Movie;
import xupt.se.ttms.service.MovieSrv;
import xupt.se.ttms.view.employee.Employee_MainUITmpl;
public class InsertMovie{
	private  static String user="";
	private static String imgurl="";
	public  InsertMovie(String user)
	{
		this.user=user;
		Init();
	}
	
	 
	public static void Init(){
	JFrame p= new JFrame();
	p.setTitle("小霸王影院");
	p.setLayout(null);
	JPanel jp= new JPanel();
	p.setSize(800,500);
	jp.setLayout(null);
	jp.setBounds(0, 0, 850, 500);
	jp.setBackground(Color.decode("#FFE4C4"));
	ImageIcon icon = new ImageIcon("mg/963.png");
	icon.setImage(icon.getImage().getScaledInstance(850, 60, Image.SCALE_DEFAULT));
	JLabel jj1= new JLabel(icon);
	jj1.setBounds(0, 0, 850,60);
	jp.add(jj1);
	JLabel j1 =new JLabel("名称:");
	j1.setFont(new Font("宋体", Font.BOLD, 20));
	j1.setBounds(260,75,55,25);
	jp.add(j1);
	JLabel j2= new JLabel("导演:");
	j2.setFont(new Font("宋体", Font.BOLD, 20));
	j2.setBounds(260,100,55,25);
	jp.add(j2);	
	JLabel j3= new JLabel("主演:");
	j3.setFont(new Font("宋体", Font.BOLD, 20));
	j3.setBounds(260,125,55,25);
	jp.add(j3);
	JLabel j4= new JLabel("类型:");
	j4.setFont(new Font("宋体", Font.BOLD, 20));
	j4.setBounds(260,150,55,25);
	jp.add(j4);
	JLabel j5= new JLabel("评分:");
	j5.setFont(new Font("宋体", Font.BOLD, 20));
	j5.setBounds(260,175,55,25);
	jp.add(j5);	
	JLabel j6= new JLabel("时长:");
	j6.setBounds(260,200,55,25);
	j6.setFont(new Font("宋体", Font.BOLD, 20));
	jp.add(j6);
	JLabel j7= new JLabel("上映日期:");
	j7.setFont(new Font("宋体", Font.BOLD, 20));
	j7.setBounds(218,225,105,30);
	jp.add(j7);
	JLabel j8= new JLabel("剧情简介:");
	j8.setFont(new Font("宋体", Font.BOLD, 20));
	j8.setBounds(218,250,105,25);
	jp.add(j8);
	JTextField t1= new  JTextField();
	t1.setBounds(312,75, 150,22);
	jp.add(t1);
	JTextField t2= new  JTextField();
	t2.setBounds(312, 100, 150,22);
	jp.add(t2);
	JTextField t3= new  JTextField();
	t3.setBounds(312, 125, 150,22);
	jp.add(t3);
	JTextField t4= new  JTextField();
	t4.setBounds(312, 150, 150,22);
	jp.add(t4);
	JTextField t5= new  JTextField();
	t5.setBounds(312,175, 150,22);
	jp.add(t5);
	
	JTextField t6 = new JTextField();
	t6.setBounds(312, 200, 150, 22);
	jp.add(t6);
	JTextField t7 = new JTextField();
	t7.setBounds(312, 230, 150, 22);
	jp.add(t7);
	JTextArea t8= new  JTextArea();
	t8.setBorder(BorderFactory.createLineBorder(Color.decode("#c0c0c0")));
	t8.setBounds(312, 255, 150,99);
	t8.setWrapStyleWord(true);
	t8.setLineWrap(true);
	jp.add(t8);
	JFileChooser fc=new JFileChooser();
    
	JButton b1 =new JButton("添加完成");
	
	b1.setFocusPainted(false);
	b1.setContentAreaFilled(false);
	b1.setBounds(312,365,150,50);
	b1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
        	if(InsertMovie.imgurl.trim().equals(""))
        	{
        		JOptionPane.showConfirmDialog(null, "影片图片为空，添加失败");
        		return;      		
        	}
        	if(t1.getText().trim().equals("")){
        		//空的情况执行
        		JOptionPane.showConfirmDialog(null, "影片名称为空，添加失败");
        		return;
        		}else{
        		//不空的情况执行
        		}
        	if(t2.getText().trim().equals("")){
        		//空的情况执行
        		JOptionPane.showConfirmDialog(null, "影片导演为空，添加失败");
        		return;
        		}else{
        		//不空的情况执行
        		}
        	if(t3.getText().trim().equals("")){
        		//空的情况执行
        		JOptionPane.showConfirmDialog(null, "影片主演为空，添加失败");
        		return;
        		}else{
        		//不空的情况执行
        		}
        	if(t4.getText().trim().equals("")){
        		//空的情况执行
        		JOptionPane.showConfirmDialog(null, "影片类型为空，添加失败");
        		return;
        		}else{
        		//不空的情况执行
        		}
        	if(t5.getText().trim().equals("")){
        		//空的情况执行
        		JOptionPane.showConfirmDialog(null, "影片评分为空，添加失败");
        		return;
        		}else{
        		//不空的情况执行
        		}
        	if(t7.getText().trim().equals("")){
        		//空的情况执行
        		JOptionPane.showConfirmDialog(null, "影片上映日期为空，添加失败");
        		return;
        		}else{
        		//不空的情况执行
        		}
        	if(t8.getText().trim().equals("")){
        		//空的情况执行
        		JOptionPane.showConfirmDialog(null, "影片简介为空，添加失败");
        		return;
        		}else{
        		//不空的情况执行
        	
        	if(t6.getText().trim().equals("")){
        		//空的情况执行
        		JOptionPane.showConfirmDialog(null, "影片时长为空，添加失败");
        		return;
        		}else{
        		//不空的情况执行
        		}
        	String img=InsertMovie.imgurl;
        	String name=t1.getText();
        	String director=t2.getText();
        	String actor=t3.getText();
        	String type=t4.getText();
        	String score=t5.getText();       	
        	String time=t6.getText();
        	String date=t7.getText();
        	String introduction=t8.getText();
        	Movie movie =new Movie();
        	movie.setImg(img);
        	movie.setName(name);
        	movie.setScore(Float.parseFloat(score));
        	movie.setDirector(director);
        	movie.setActor(actor);
        	movie.setType(type);
        	movie.setTime(time);
        	movie.setDate(Integer.parseInt(date));
        	movie.setIntroduction(introduction);
        	MovieSrv movieSrv=new MovieSrv();
        	try {
				movieSrv.add(movie);
				if(Employee_MainUITmpl.jTable!=null)
				{
					Employee_MainUITmpl.jTable.updateUI();
				}
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
        	p.dispose();
        	Employee_MainUITmpl employee_MainUITmpl=new Employee_MainUITmpl(user);
        }}
    });
	jp.add(b1);
	
	 JLabel img1= new JLabel("           添加图片");
	 img1.setBorder(BorderFactory.createLineBorder(Color.decode("#c0c0c0")));
	 img1.setBounds(70,160, 130, 180);
	 jp.add(img1);
	 img1.addMouseListener(new  MouseListener() {
		
		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO 自动生成的方法存根
			
		}
		
		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO 自动生成的方法存根
			
		}
		
		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO 自动生成的方法存根
			
		}
		
		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO 自动生成的方法存根
			
		}
		
		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO 自动生成的方法存根
			 JFileChooser jfc = new JFileChooser();
    	     if(jfc.showOpenDialog(p)==JFileChooser.APPROVE_OPTION ){
    	     File file=jfc.getSelectedFile();
    	     String URL=file.getPath();
    	     InsertMovie.imgurl=URL;
			 ImageIcon image = new ImageIcon(URL);
             img1.setIcon(image);
             
		}
		}
	});
	 	
	p.add(jp);
	p.setLocationRelativeTo(null);
	p.setResizable(false);
	p.setVisible(true);
	}
	

}
